package com.iwomy.envpro.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.admin.api.feign.RemoteDeptService;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.AirAlarmSettingEntity;
import com.iwomy.envpro.platform.entity.AirGridMemberEntity;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.service.AirGridMemberService;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.AirSiteService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人员管理
 *
 * @author pig
 * @date 2023-08-26 10:16:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/airGridMember")
@Tag(description = "airGridMember", name = "人员管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AirGridMemberController {

	private final AirGridMemberService airGridMemberService;


	private final AirMonitorEquipmentService airMonitorEquipmentService;

	private final AirSiteService airSiteService;

	private final RemoteDeptService remoteDeptService;

	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param airGridMember 人员管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_view')")
	public R getAirGridMemberPage(@ParameterObject Page page, @ParameterObject AirGridMemberEntity airGridMember) {
		return R.ok(airGridMemberService.getAirGridMemberPage(page, airGridMember));
	}


	/**
	 * 通过id查询人员管理
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(airGridMemberService.getById(id));
	}

	/**
	 * 新增人员管理
	 *
	 * @param airGridMember 人员管理
	 * @return R
	 */
	@Operation(summary = "新增人员管理", description = "新增人员管理")
	@SysLog("新增人员管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_add')")
	public R save(@RequestBody AirGridMemberEntity airGridMember) {
		airGridMemberService.addAirGridMember(airGridMember);

		return R.ok(airGridMemberService.save(airGridMember));
	}

	/**
	 * 修改人员管理
	 *
	 * @param airGridMember 人员管理
	 * @return R
	 */
	@Operation(summary = "修改人员管理", description = "修改人员管理")
	@SysLog("修改人员管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_edit')")
	public R updateById(@RequestBody AirGridMemberEntity airGridMember) {
		LambdaQueryWrapper<AirSiteEntity> wrapper = Wrappers.<AirSiteEntity>lambdaQuery();
		wrapper.eq(AirSiteEntity::getPid, airGridMember.getId());

		List<AirSiteEntity> airSiteList = airSiteService.list(wrapper);
		for (AirSiteEntity airSite : airSiteList) {
			airSite.setPname(airGridMember.getName());
		}
		airSiteService.updateBatchById(airSiteList);
		return R.ok(airGridMemberService.updateById(airGridMember));
	}

	/**
	 * 通过id删除人员管理
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除人员管理", description = "通过id删除人员管理")
	@SysLog("通过id删除人员管理")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_del')")
	public R removeById(@RequestBody Long[] ids) {

		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getGridMemberId, ids));

		if (count > 0)
			return R.failed("人员已被使用不允许删除");

		return R.ok(airGridMemberService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param airGridMember 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_export')")
	public List<AirGridMemberEntity> export(AirGridMemberEntity airGridMember) {
		return airGridMemberService.list(Wrappers.query(airGridMember));
	}

	/**
	 * @param airGridMember 查询条件
	 */
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_airGridMember_export')")
	public R list(AirGridMemberEntity airGridMember) {
		return R.ok(airGridMemberService.list(Wrappers.query(airGridMember)));
	}

	/**
	 * @param deptId 查询条件
	 */
	@GetMapping("/listByDept/{deptId}")
	public R listByDept(@PathVariable("deptId") Long deptId) {
		LambdaQueryWrapper<AirGridMemberEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(AirGridMemberEntity::getDeptId, remoteDeptService.listByDeptId(deptId).getData());
		return R.ok(airGridMemberService.list(queryWrapper));
	}
	/**
	 * @param deptId 查询条件
	 */
	@GetMapping("/listByThisLevel/{deptId}")
	public R listByThisLevel(@PathVariable("deptId") Long deptId) {
		LambdaQueryWrapper<AirGridMemberEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(AirGridMemberEntity::getDeptId,deptId);
		return R.ok(airGridMemberService.list(queryWrapper));
	}

/*********************************APP接口*******************************************/
	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param airGridMember 人员管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/appAirGridMemberPage")
	public R appAirGridMemberPage(@ParameterObject Page page, @ParameterObject AirGridMemberEntity airGridMember) {
		LambdaQueryWrapper<AirGridMemberEntity> wrapper = Wrappers.lambdaQuery(airGridMember);
		return R.ok(airGridMemberService.page(page, wrapper));
	}

}